package org.senf.outerspace.ships;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.senf.outerspace.ships.design.IEngineBluePrint;
import org.senf.outerspace.ships.design.IModelBluePrint;
import org.senf.outerspace.ships.design.IWeaponBluePrint;

public class BluePrintFactory {

	private static BluePrintFactory instance;
	
	public static BluePrintFactory getInstance() {
		if (instance == null) {
			instance = new BluePrintFactory();
		}
		return instance;
	}
	
	private BluePrintFactory() {
		// Private Constructor
	}
	
	public IModelBluePrint getModelBP(String id) {
		List<IModelBluePrint> items = getAvailableModels();
		for (IModelBluePrint item: items) {
			if (item.getId().equals(id)) return item;
		}
		return null;
	}
	
	public IEngineBluePrint getEngineBP(String id) {
		List<IEngineBluePrint> items = getAvailableEngines();
		for (IEngineBluePrint item: items) {
			if (item.getId().equals(id)) return item;
		}
		return null;
	}
	
	public IWeaponBluePrint getWeaponBP(String id) {
		List<IWeaponBluePrint> items = getAvailableWeapons();
		for (IWeaponBluePrint item: items) {
			if (item.getId().equals(id)) return item;
		}
		return null;
	}
	
	public List<IModelBluePrint> getAvailableModels() {
		List<IModelBluePrint> items = new ArrayList<IModelBluePrint>();
		
		IConfigurationElement[] config = Platform.getExtensionRegistry()
			.getConfigurationElementsFor("org.senf.outerspace.ships.model");
		
		try {
			for (IConfigurationElement e : config) {
				IModelBluePrint item = (IModelBluePrint)e.createExecutableExtension("blueprint");
				item.setId(e.getAttribute("id"));
				items.add(item);
			}
		} catch (CoreException ex) {
			System.out.println(ex.getMessage());
		}

		return items;
	}
	
	public List<IEngineBluePrint> getAvailableEngines() {
		List<IEngineBluePrint> items = new ArrayList<IEngineBluePrint>();
		
		IConfigurationElement[] config = Platform.getExtensionRegistry()
			.getConfigurationElementsFor("org.senf.outerspace.ships.engine");
		
		try {
			for (IConfigurationElement e : config) {
				IEngineBluePrint item = (IEngineBluePrint)e.createExecutableExtension("blueprint");
				item.setId(e.getAttribute("id"));
				items.add(item);
			}
		} catch (CoreException ex) {
			System.out.println(ex.getMessage());
		}

		return items;
	}
	
	public List<IWeaponBluePrint> getAvailableWeapons() {
		List<IWeaponBluePrint> items = new ArrayList<IWeaponBluePrint>();
		
		IConfigurationElement[] config = Platform.getExtensionRegistry()
			.getConfigurationElementsFor("org.senf.outerspace.ships.weapon");
		
		try {
			for (IConfigurationElement e : config) {
				IWeaponBluePrint item = (IWeaponBluePrint)e.createExecutableExtension("blueprint");
				item.setId(e.getAttribute("id"));
				items.add(item);
			}
		} catch (CoreException ex) {
			System.out.println(ex.getMessage());
		}

		return items;
	}
}
